﻿package communication1.pfl;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class UploadActivity extends Activity {
	public static final int PHOTOHRAPH = 4;// 拍照
	public static final int PHOTOHRAPH1 = 9;
	int biaozhi = 0;// 总开关
	int flagt = 0;// 文字发送标识
	int flagp = 0;// 图片发送标识
	int flagv = 0;// 视频发送标识
	int flagp2 = 0;// 图片发送标识
	int flagv2 = 0;// 视频发送标识
	String pic_name = "";
	String video_name = "";

	String pic_name2 = "";
	String video_name2 = "";
	int count111 = 0;
	String zhaopianlujing;
	private static final int DIALOG = 99;
	private static final int DIALOG1 = 100;
	private static final int DIALOG10 = 101;
	private static final int DIALOG11 = 102;
	private static final int DIALOG12 = 103;
	private static final int DIALOG13 = 104;
	private static final int DIALOG14 = 105;
	private static final int DIALOG15 = 106;
	private static final int DIALOG16 = 107;
	private static final int DIALOG17 = 108;
	String shipinlujing;
	private EditText desc = null;
	private EditText remark_content = null;
	private TextView capture = null, record_video = null;
	private Uri photoUri;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.remark);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		findViewById(R.id.finish).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						setResult(RESULT_OK);
						finish();
					}
				});

		{
			desc = (EditText) findViewById(R.id.desc);
			remark_content = (EditText) findViewById(R.id.remark_content);
			capture = (TextView) findViewById(R.id.capture);
			record_video = (TextView) findViewById(R.id.record_video);

		}
		{

			findViewById(R.id.begin_record).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							Toast.makeText(
									UploadActivity.this,
									"your data:"
											+ remark_content.getText()
													.toString(),
									Toast.LENGTH_SHORT).show();
						}
					});

			findViewById(R.id.goback).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			findViewById(R.id.finish).setVisibility(View.GONE);
		}
		{
			capture = (TextView) findViewById(R.id.capture);
			capture.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					capture();
				}

			});
			record_video = (TextView) findViewById(R.id.record_video);
			record_video.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					record_video();
				}
			});
		}
	}

	private void capture() {
		Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		ContentValues values = new ContentValues(); // 创建一个内容提供者
		photoUri = UploadActivity.this.getContentResolver().insert(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
		imageCaptureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
				photoUri);
		startActivityForResult(imageCaptureIntent, PHOTOHRAPH);
	}

	public void record_video() {
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
		startActivityForResult(intent, PHOTOHRAPH1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// switch (resultCode) {
		// case RESULT_OK:
		// finish();
		// break;
		//
		// default:
		// super.onActivityResult(requestCode, resultCode, data);
		// break;
		// }

		switch (requestCode) {
		case PHOTOHRAPH:

			if (resultCode == RESULT_OK) {

				flagp = 1;
				flagp2 = 1;
				Log.v("pfl", "准备获取数据:" + data);

				ContentResolver cr = this.getContentResolver();
				Cursor cursor = cr.query(photoUri, null, null, null, null);
				cursor.moveToFirst(); // 游标移到第一条就是系统默认的路径
				if (cursor != null) {
					zhaopianlujing = cursor.getString(1); // 获取到的路径可以用于图片自定义压缩了
					cursor.close();
					Log.v("pfl", "照片路径是：" + zhaopianlujing);
				}

				pic_name = new SimpleDateFormat("yyyyMMddHHmmss")
						.format(new Date()) + ".jpg";// 照片命名

				try {
					capture.setText("已拍照");
					EditText txt = (EditText) findViewById(R.id.edit10);
					txt.setText("已拍照");
				} catch (Exception e) {

				}
				try {
					EditText txt1 = (EditText) findViewById(R.id.pic2_text);
					// record_video.setText("已拍照");
				} catch (Exception e) {

				}
			}

			break;
		case PHOTOHRAPH1:

			if (resultCode == RESULT_OK) {
				flagv = 1;
				flagv2 = 1;
				Uri uriVideo = data.getData();
				Cursor cursor = this.getContentResolver().query(uriVideo, null,
						null, null, null);
				if (cursor.moveToNext()) // 实际只查询了一次
				{
					shipinlujing = cursor.getString(cursor
							.getColumnIndex("_data"));
					video_name = new SimpleDateFormat("yyyyMMddHHmmss")
							.format(new Date()) + ".3gp";
				}

				try {
					EditText txt = (EditText) findViewById(R.id.edit11);
					// txt.setText("已摄像");
					record_video.setText("已摄像");
				} catch (Exception e) {

				}
				try {
					EditText txt1 = (EditText) findViewById(R.id.vid2_text);
					txt1.setText("已摄像");
				} catch (Exception e) {

				}

			}
			break;
		case 21:
			if (resultCode == RESULT_OK) {
				finish();
			}
			break;
		default:
			break;
		}

	}
}
